using System;
using SampleNs;
using System.DHTML;

namespace ClientScripts
{
    /// <summary>
    /// Sample Application
    /// 
    /// TODO: Why does this only work in Firefox
    /// </summary>
    public class AppMain
    {
        public void Init()
        {            
            InputElement buttonElement = (InputElement) Document.CreateElement("input");
            buttonElement.Type = "button";
            buttonElement.Value = "GetSample";
            buttonElement.AttachEvent(
                "onclick", 
                new DOMEventHandler(
                    delegate()
                    {
                        SampleService.GetSample(
                            new GetSampleSuccessCallbackDelegate(this.SampleSuccess),
                            new GetSampleFailureCallbackDelegate(this.SampleFailure)
                            );
                    }
                ));

            Document.DocumentElement.AppendChild(buttonElement);

            DivElement divElement = (DivElement)Document.CreateElement("div");

            InputElement idInput = (InputElement)Document.CreateElement("input");
            idInput.Type = "text";

            InputElement getByIdButtonElement = (InputElement)Document.CreateElement("input");
            getByIdButtonElement.Type = "button";
            getByIdButtonElement.Value = "GetSampleById";
            getByIdButtonElement.AttachEvent(
                "onclick",
                new DOMEventHandler(
                    delegate()
                    {
                        SampleService.GetSampleById(
                            (int)Int32.Parse(idInput.Value),
                            new GetSampleByIdSuccessCallbackDelegate(this.GetSampleSuccessCallbackDelegate),
                            new GetSampleByIdFailureCallbackDelegate(this.GetSampleFailureCallbackDelegate)
                            );
                    }
                ));

            divElement.AppendChild(idInput);
            divElement.AppendChild(getByIdButtonElement);
            Document.DocumentElement.AppendChild(divElement);
        }

        private void Example(){
            SampleService.GetSample(                           
                            new GetSampleSuccessCallbackDelegate(
                                this.GetSampleSuccessCallbackDelegate
                                ),
                            new GetSampleFailureCallbackDelegate(
                                this.GetSampleFailureCallbackDelegate)
                            );
        }

        /// <summary>
        /// The method is called an a success
        /// </summary>
        /// <param name="sample">The object being returned</param>
        /// <param name="args">Ignor for now; will have mapped object later</param>
        private void GetSampleSuccessCallbackDelegate(
            Sample sample, object args){
            
            //Notice the object is known type and we can access their properties
            Script.Alert(
                string.Concat( 
                    sample.BaseName,
                    "- ", 
                    sample.Description)
                );
        }

        /// <summary>
        /// This method is called on a failure
        /// </summary>
        /// <param name="error"></param>
        private void GetSampleFailureCallbackDelegate(object error) {
            //Alert the failure
            Script.Alert("Failed");
        }


        private void SampleSuccess(Sample sample, object args)
        {
            
            DivElement div = (DivElement) Document.CreateElement("div");
            div.InnerText = string.Concat( sample.Id , " - ", sample.Name);
            Document.DocumentElement.AppendChild(div);

            TableElement table = (TableElement)Document.CreateElement("table");
            table.Style.Width = "100%";
            table.Style.BorderWidth = "1px";
            table.Style.Border = "1px";
            foreach (Sample otherSample in sample.OtherSamples)
            {
                TableRowElement row = (TableRowElement)Document.CreateElement("tr");

                TableCellElement idCell = (TableCellElement)Document.CreateElement("td");
                idCell.InnerText = otherSample.Id.ToString();
                row.AppendChild(idCell);

                TableCellElement nameCell = (TableCellElement)Document.CreateElement("td");
                nameCell.InnerText = otherSample.Name;
                row.AppendChild(nameCell);

                table.AppendChild(row);
                
            }

            div.AppendChild(table);
            
        }
        private void SampleFailure(WebServiceException error)
        {
            Script.Alert(error.StackTrace);
        }

        public static void main(Dictionary args)
        {
            AppMain appMain = new AppMain();
            appMain.Init();
        } 
      


        
    }
    

}
